<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class S_admin extends Page_Controller{

	function S_admin(){
        parent::Page_Controller();
        $this->CI = &get_instance();
        $this->use_global_template(FALSE);
	}
	
	function index(){
		$this->CI->load->module_library('users.auth');
        if($this->CI->auth->is_logged())
            $this->CI->auth->forward('admin/home');

        $this->CI->load->library('form_validation');
        $this->CI->load->helper('form');
        
        $this->CI->form_validation->set_rules('username', 'Username', 'trim|required');
		$this->CI->form_validation->set_rules('password', 'Password', 'trim|required');

		if ($this->CI->form_validation->run() !== FALSE){
            $this->CI->load->module_model('users.user');
            $result = $this->CI->user->superadmin_login($this->CI->input->post('username'), $this->CI->input->post('password'));
            if($result == 0){
                $this->CI->form_validation->set_error('username', 'The Username not found');
            }elseif($result == -1){
                $this->CI->form_validation->set_error('password', 'Invalid password');
            }else{
                $result['is_superadmin'] = 1;
                $result['admin_id'] = 0;
                $result['name'] = $this->CI->input->post('username');
                $this->CI->auth->logged($result);
            }
        }
	}
}